package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page =  setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!Collections.isEmpty(setmealDishes)){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
            setmealDishMapper.insert(setmealDishes);
        }
    }

    /**
     * 删除套餐
     */
    @Override
    public void delete(List<Long> ids) {
        if (Collections.isEmpty(ids)){
            throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }
        List<Setmeal> setmealList = setmealMapper.queryStatusByIds(ids);
        if (!Collections.isEmpty(setmealList)){
            throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }

    /**
     * 根据id查询套餐
     */
    @Override
    public SetmealVO queryById(Integer id) {
        SetmealVO setmealVO = setmealMapper.queryById(id);
        List<SetmealDish> setmealDishList = setmealDishMapper.queryById(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 修改套餐
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDisheList = setmealDTO.getSetmealDishes();
        setmealDisheList.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        if (!Collections.isEmpty(setmealDisheList)){
            setmealDishMapper.delete(java.util.Collections.singletonList(setmealDTO.getId()));
            setmealDishMapper.insert(setmealDisheList);
        }
    }

    /**
     * 起售停售
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        if (status == 1){
            List<SetmealDish> setmealDishList = setmealDishMapper.queryById(Math.toIntExact(id));
            List<Long> dishIdList = setmealDishList.stream().map(SetmealDish::getDishId).toList();
            List<Dish> dishes = dishMapper.queryDishStatus(dishIdList);
            if (!Collections.isEmpty(dishes)){
                throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.update(setmeal);
    }

    /**
     * 根据套餐id查询包含的菜品
     */
    @Override
    public List<DishItemVO> querySetmealDish(Integer setmealId) {
        return setmealDishMapper.querySetmealDish(setmealId);
    }

    /**
     * 根据分类id查询套餐
     */
    @Override
    public List<Setmeal> queryByCategoryId(Long categoryId) {
        return setmealMapper.queryByCategoryId(categoryId);
    }
}
